package com.sumfun.blog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sumfun.blog.common.BaseResponse;
import com.sumfun.blog.common.ErrorCode;
import com.sumfun.blog.common.PageRequest;
import com.sumfun.blog.common.ResultUtils;
import com.sumfun.blog.exception.BusinessException;
import com.sumfun.blog.model.domain.TimeLine;
import com.sumfun.blog.service.TimeLineService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 时间轴Controller
 *
 * @author sumfun
 * @date 2024-04-21
 */
@Api(value = "时间轴信息Controller", tags = {"时间轴信息"})
@RestController
@RequestMapping("/timeLine")
public class TimeLineController {

    @Resource
    private TimeLineService timeLineService;

    @PostMapping("/add")
    public BaseResponse<Boolean> addTimeLine(@RequestBody TimeLine timeLine, HttpServletRequest request){
        //判空
        if(timeLine == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean timeLineId = timeLineService.save(timeLine);
        return ResultUtils.success(timeLineId);
    }

    @DeleteMapping("/{id}")
    public BaseResponse<Boolean> deleteTimeLine(@PathVariable long id) {
        if(id < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = timeLineService.removeById(id);
        return ResultUtils.success(res);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updateTimeLine(@RequestBody TimeLine timeLine, HttpServletRequest request) {
        if(timeLine == null || timeLine.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = timeLineService.updateById(timeLine);
        return ResultUtils.success(res);
    }

    @GetMapping("/get")
    public BaseResponse<TimeLine> getTimeLineById(long id) {
        if(id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        TimeLine timeLine = timeLineService.getById(id);
        return ResultUtils.success(timeLine);
    }

    @GetMapping("/list")
    public BaseResponse<List<TimeLine>> getTimeLineList() {
        List<TimeLine> timeLineList;
        timeLineList = timeLineService.list();
        return ResultUtils.success(timeLineList);
    }
    //
    @GetMapping("/list/page")
    public BaseResponse<Page> getTimeLineListByPage(PageRequest timeLineQueryRequest) {
        Page<TimeLine> page = new Page(timeLineQueryRequest.getPageNum(), timeLineQueryRequest.getPageSize());
        Page<TimeLine> page1 = timeLineService.page(page);
        return ResultUtils.success(page1);
    }

    @GetMapping("/tree")
    public BaseResponse<List> getTimeLineTree() {
        // If parentId is null, we assume we want the root directories
        List timeLineTree = timeLineService.getTimeLineTree();
        return ResultUtils.success(timeLineTree);

    }
}
